<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="园区" prop="area">
        <el-select v-model="queryParams.area" placeholder="请选择园区"  style="width: 240px" @change="handleQuery">
          <el-option label="全部" value="" ></el-option>
          <el-option v-for="item in dict.type.user_area" :label="item.label" :value="item.value" ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="检测项目" prop="project"  v-show="showSearch">
        <el-input v-model="queryParams.project" placeholder="请输入检测项目" clearable @clear="handleQuery" />
      </el-form-item>
      <el-form-item  v-show="showSearch">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-form-item>
      <!--      <el-form-item >-->
      <!--        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>-->
      <!--      </el-form-item>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-form>
    <el-table v-loading="loading" :data="recordList" >
      <el-table-column label="编号" align="center" prop="id" width="50"/>
      <el-table-column label="检测项目" align="center" prop="project" min-width="180" />
      <el-table-column label="园区" align="center" prop="area" width="120"/>
      <el-table-column label="检测单位" align="center" prop="enterprise" width="150"/>
      <el-table-column label="送检日期" align="center" prop="qdate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.qdate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="检测性质" align="center" prop="nature" width="150"/>
      <el-table-column label="检测依据" align="center" prop="accord" width="150"/>
      <el-table-column label="检测仪器" align="center" prop="device" width="150"/>
      <el-table-column label="图片" align="center" prop="imageUrl" :width="imageColWidth">
        <template v-slot:default="scope">
          <image-preview v-for="(item,index) in scope.row.imageUrl"
                    style="width: 100px; height: 100px; margin-right: 15px"
                    :src="item">
          </image-preview>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" width="150"/>
      <el-table-column label="创建人" prop="createBy" width="100" align="center"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新人" prop="updateBy" width="100" align="center"/>
      <el-table-column label="修改时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" >修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>
    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="检测项目" prop="project">
              <el-input v-model="form.project" placeholder="请输入检测项目" />
            </el-form-item>
            <el-form-item label="检测单位" prop="enterprise">
              <el-input v-model="form.enterprise" placeholder="请输入检测单位" />
            </el-form-item>
            <el-form-item label="检测性质" prop="nature">
              <el-input v-model="form.nature" placeholder="请输入检测性质" />
            </el-form-item>
            <el-form-item label="检测仪器" prop="device">
              <el-input v-model="form.device" placeholder="请输入检测仪器" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="园区" prop="area">
              <el-select v-model="form.area" placeholder="请选择园区" style="width: 100%;">
                <el-option  label="二区" value="二区"/>
                <el-option  label="一区" value="一区"/>
              </el-select>
            </el-form-item>
            <el-form-item label="送检日期" prop="qdate">
              <el-date-picker clearable v-model="form.qdate" type="datetime" format="yyyy-MM-dd HH:MM:SS" value-format="yyyy-MM-dd HH:MM:SS" style="width: 100%" placeholder="请选择送检日期">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="检测依据" prop="accord">
              <el-input v-model="form.accord" placeholder="请输入检测依据" />
            </el-form-item>
            <el-form-item label="备注" prop="accord">
              <el-input v-model="form.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="检验图片" prop="imageUrl">
          <ImageUpload
            v-model="form.imageUrl"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listQualityRecord, getQualityRecord, delQualityRecord, addQualityRecord, updateQualityRecord } from "@/api/store/quality";
import {blurRule, changeRule} from "@/utils/formRules";

export default {
  name: "Examine",
  dicts:['user_area'],
  data() {
    return {
      imageColWidth: 150,
      baseUrl: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        area: '',
        enterprise: null,
        nature: null,
        project: null,
        qdate: null,
        accord: null,
        device: null,
        imageUrl: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        area:changeRule('请选择园区'),
        project:blurRule('请输入检项目')
      }
    };
  },
  created() {
    this.queryParams.area=this.$store.state.user.userInfo.area || ''
    this.getList();
  },
  methods: {
    /** 查询【请填写功能名称】列表 */
    getList() {
      this.loading = true;
      listQualityRecord(this.queryParams).then(response => {
        let maxWith = 1;
        for (var i = 0; i < response.rows.length; i++) {
          response.rows[i].imageurl = response.rows[i].imageUrl;
          response.rows[i].imageUrl = [];
          if (response.rows[i].imageurl != null && response.rows[i].imageurl != undefined) {
            response.rows[i].imageUrl = response.rows[i].imageurl.split(",");
          }
          if (response.rows[i].imageUrl.length > maxWith) {
            maxWith = response.rows[i].imageUrl.length;
          }
        }
        this.imageColWidth = maxWith * 150;
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        // id: null,
        area: null,
        enterprise: null,
        nature: null,
        project: null,
        qdate: null,
        accord: null,
        device: null,
        imageUrl: null,
        remark:null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加检测台账";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      // const id = row.id
      getQualityRecord(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改检测台账";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id) {
            updateQualityRecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addQualityRecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      // const ids = row.id ;
      this.$modal.confirm('是否确认删除台账检测单编号为"' + row.id + '"的数据项？').then(function() {
        return delQualityRecord(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/record/export', {
        ...this.queryParams
      }, `检测台账_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
